/*
 * ml20gui - The MSN Webcam Recorder
 * This source code is under the GNU Public License (GPL).
 * Written by Ramiro Polla
 * Initial release: june 3rd, 2005
 *
 * this program records MSN Webcam Video
 */

#include "ml20gui.h"

char dlgfn[MAX_PATH];

void *GetDlgListItem( HWND hwnd, int id, int index )
{
    LVITEM two;
    void *item;

    memset( &two, 0, sizeof(LVITEM) );
    two.mask = LVIF_PARAM;
    two.iItem = index;
    SendDlgItemMessage( hwnd, id, LVM_GETITEM, 0, (LPARAM) &two );

    item = ((void *) two.lParam);

    return item;
}

void SetDlgListImage( HWND hwnd, int id, int index, int image )
{
    LVITEM two;

    memset( &two, 0, sizeof(LVITEM) );
    two.iItem = index;
    two.iSubItem = 0;
    two.mask = LVIF_IMAGE;
    two.iImage = image;

    SendDlgItemMessage( hwnd, id, LVM_SETITEM, 1, (long) &two );
}

char *SaveFileDlg( HWND hwnd, char *filter, char *ext, char *fname )
{
    OPENFILENAME ofn;

    memset( &ofn, 0, sizeof(OPENFILENAME) );

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = (char *) &dlgfn;
    memset( dlgfn, 0, sizeof( dlgfn ) );
    if( fname )
        strcpy( dlgfn, fname );

    ofn.nMaxFile = sizeof( dlgfn );
    ofn.lpstrFilter = filter;
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_OVERWRITEPROMPT | OFN_EXTENSIONDIFFERENT;
    ofn.lpstrDefExt = ext;

    if( GetSaveFileName( &ofn ) == 0 )
        return 0;

    return dlgfn;
}

char *OpenFileDlg( HWND hwnd, char *filter, char *ext )
{
    OPENFILENAME ofn;

    memset( &ofn, 0, sizeof(OPENFILENAME) );

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = (char *) &dlgfn;

    memset( dlgfn, 0, sizeof(dlgfn) );
    ofn.nMaxFile = sizeof(dlgfn);
    ofn.lpstrFilter = filter;
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_FILEMUSTEXIST;
    ofn.lpstrDefExt = ext;

    if( GetOpenFileName( &ofn ) == 0 )
        return 0;

    return dlgfn;
}

int GetDlgListSelection( HWND hwnd, int id )
{
    int index;
    index = SendDlgItemMessage( hwnd, id, LVM_GETSELECTIONMARK, 0, 0 );
    if( index == (-1) )
        return -1;
    else
        if( SendDlgItemMessage( hwnd, id, LVM_GETITEMSTATE, index, LVIS_SELECTED ) == 0 )
            return -1;
        else
            return index;
}

void lvcolumn_text( HWND hwnd, int ID, int subitem, char *text )
{
    LVCOLUMN lvc;
    lvc.mask = LVCF_TEXT;
    lvc.iSubItem = subitem;
    lvc.pszText = text;
    SendDlgItemMessage( hwnd, ID, LVM_SETCOLUMN, subitem, (long) &lvc );
}

void add_lvcolumn( HWND hwnd, int ID, int subitem, char *text, int cx, int fmt )
{
    LVCOLUMN lvc;
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvc.iSubItem = subitem;
    lvc.pszText = text;
    lvc.cx = cx;
    lvc.fmt = fmt;
    SendDlgItemMessage( hwnd, ID, LVM_INSERTCOLUMN, subitem, (long) &lvc );
}

int GetDlgButtonCheck( HWND hwnd, int id )
{
    if( SendDlgItemMessage( hwnd, id, BM_GETCHECK, 0, 0 ) == BST_CHECKED )
        return TRUE;
    else
        return FALSE;
}
